package org.sg20.graphbuilder;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.sg20.labelpropagation.lpaiter.LpaIterDriver;
import org.sg20.labelpropagation.lpaiter.LpaIterMapper;
import org.sg20.labelpropagation.lpaiter.LpaIterReducer;

import java.io.IOException;

public class GraphBuilderDriver {
    /**
     * @param personRelationFile 上一任务的输出文件
     * @param outPath 输出路径
     */

    public  static  String main(String personRelationFile, String outPath) throws IOException, InterruptedException, ClassNotFoundException{
        Configuration conf = new Configuration();

        FileSystem fs=FileSystem.get(conf);
        //若输出文件夹已存在需要进行删除
        if(fs.exists(new Path(outPath)))
            fs.delete(new Path(outPath), true);

        Job job = Job.getInstance(conf);
        job.setJobName("GraphBuilder");

        // 2 关联本Driver程序的jar
        job.setJarByClass(GraphBuilderDriver.class);

        // 3 关联Mapper和Reducer的jar
        job.setMapperClass(GraphBuilderMapper.class);
        job.setReducerClass(GraphBuilderReducer.class);


        // 4 设置Mapper输出的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        // 5 设置最终输出kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        // 6 设置输入和输出路径
        FileInputFormat.setInputPaths(job, new Path(personRelationFile));
        FileOutputFormat.setOutputPath(job, new Path(outPath));

        // 7 提交job
        job.waitForCompletion(true);
        //return DigestUtils.md5Hex(fs.open(new Path(outPath+"/part-r-00000")));
        return  null;
    }
}
